package cn.xm.canting.adapter;

import java.util.ArrayList;
import java.util.List;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.djp.canting.R;
import cn.xm.canting.activity.SelectActivity;
import cn.xm.canting.bean.AllFood;

//用于在点餐界面显示全部商品信息的listview的adapter
public class SelectItemAdapter extends BaseAdapter{

    private List<AllFood> objects = new ArrayList<AllFood>();
    private SelectActivity context;
    private LayoutInflater layoutInflater;

    public SelectItemAdapter(SelectActivity context,List<AllFood> objects) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public AllFood getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.select_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((AllFood) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final AllFood object, final ViewHolder holder) {
        holder.selectName.setText(object.getFoodName());
        holder.selectContent.setText(object.getFoodContent());
        holder.selectPrice.setText(object.getFoodPrice()+"元/份");
        holder.selectImg.setImageResource(object.getImgId());

        //点击+时,让商品数量加1
        holder.selectAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(holder.selectNum.getText().toString());
                num++;
                holder.selectNum.setText(num+"");
                context.allallPrice += object.getFoodPrice();
                context.updataAllPrice();
                object.setNum(num);
            }
        });

        //点击-时,让商品数量减1
        holder.selectDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(holder.selectNum.getText().toString());
                //只有数量不为0时才能减少数量
                if (num != 0) {
                    num--;
                    holder.selectNum.setText(num+"");
                    context.allallPrice -= object.getFoodPrice();
                    context.updataAllPrice();
                    object.setNum(num);
                } else {
                    //当数量为0时提示用户不能减少
                    Toast.makeText(context, "数量不能低于0", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    protected class ViewHolder {
        private ImageView selectImg;
        private TextView selectName;
        private TextView selectContent;
        private TextView selectPrice;
        private TextView selectAdd;
        private TextView selectNum;
        private TextView selectDel;

        public ViewHolder(View view) {
            selectImg = (ImageView) view.findViewById(R.id.select_img);
            selectName = (TextView) view.findViewById(R.id.select_name);
            selectContent = (TextView) view.findViewById(R.id.select_content);
            selectPrice = (TextView) view.findViewById(R.id.select_price);
            selectAdd = (TextView) view.findViewById(R.id.select_add);
            selectNum = (TextView) view.findViewById(R.id.select_num);
            selectDel = (TextView) view.findViewById(R.id.select_del);
        }
    }


}
